package com.ynu.yx.chatroleai.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ynu.yx.chatroleai.converter.CharacterSkillConverter;
import com.ynu.yx.chatroleai.dao.AICharacterDao;
import com.ynu.yx.chatroleai.dao.CharacterSkillDao;
import com.ynu.yx.chatroleai.dto.CharacterSkillCreateDto;
import com.ynu.yx.chatroleai.dto.CharacterSkillUpdateDto;
import com.ynu.yx.chatroleai.dto.CharacterSkillViewDto;
import com.ynu.yx.chatroleai.model.AICharacter;
import com.ynu.yx.chatroleai.model.CharacterSkill;
import com.ynu.yx.chatroleai.utils.IdGenerator;
import com.ynu.yx.chatroleai.utils.MsgOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class CharacterSkillService {
    @Resource
    private CharacterSkillService characterSkillService;
    @Resource
    private AICharacterDao aiCharacterDao;
    @Resource
    private CharacterSkillConverter characterSkillConverter;
    @Resource
    private IdGenerator idGenerator;
    @Resource
    private CharacterSkillDao characterSkillDao;

    // ------------------------------- 增 ---------------------------------
    public MsgOperation addCharacterSkill(CharacterSkillCreateDto CSCDto) {
        if (CSCDto == null) {
            return new MsgOperation("-1", "无有效的角色技能信息");
        }
        if (CSCDto.getAiId() == null) {
            return new MsgOperation("-2", "角色ID为空");
        }
        if (CSCDto.getSkill() == null) {
            return new MsgOperation("-3", "技能为空");
        }
        if (CSCDto.getSkillConfig() == null || CSCDto.getSkillConfig().length() < 5) {
            return new MsgOperation("-4", "技能配置为空或技能配置过少");
        }
        if (aiCharacterDao.selectOneByAiId(CSCDto.getAiId()) == null){
            return new MsgOperation("-5", "角色不存在");
        }
        CharacterSkill characterSkill = characterSkillConverter.toModel(CSCDto);

        characterSkill.setId(idGenerator.generateUniqueId(characterSkillDao, "skill_id"));
        if (characterSkill.getId() == null) {
            return new MsgOperation("-6", "生成技能ID失败");
        }
        characterSkill.setIsDeleted(0);

        characterSkillDao.insert(characterSkill);
        return new MsgOperation("1", "添加角色技能成功");
    }


    // ------------------------------- 查 ---------------------------------
    // 通过AIID获取角色技能列表
    public List<CharacterSkillViewDto> getCharacterSkills(Long aiId) {
        QueryWrapper<CharacterSkill> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ai_id", aiId);

        return characterSkillConverter.toViewDto(characterSkillDao.selectList(queryWrapper));
    }


    // ------------------------------- 改 ---------------------------------
    // 通过技能ID：更新角色技能
    public MsgOperation updateCharacterSkill(String skillId, CharacterSkillUpdateDto CSUDto) {
         if (CSUDto == null) {
             return new MsgOperation("-1", "无有效的角色技能信息");
         }
         if (skillId == null) {
             return new MsgOperation("-2", "技能ID为空");
         }
         CharacterSkill characterSkill = characterSkillConverter.toModelFromUpdate(CSUDto);
         UpdateWrapper<CharacterSkill> updateWrapper = new UpdateWrapper<>();
         updateWrapper.eq("id", skillId);
         characterSkillDao.update(characterSkill, updateWrapper);
         return new MsgOperation("1", "更新角色技能成功");
    }


    // ------------------------------- 删 ---------------------------------
    // 通过技能ID：删除角色技能
    public MsgOperation deleteCharacterSkill(Long skillId) {
        if (skillId == null) {
            return new MsgOperation("-1", "技能ID为空");
        }
        if (characterSkillDao.selectBySkillId(skillId) == null) {
            return new MsgOperation("-2", "角色技能不存在");
        }

        UpdateWrapper<CharacterSkill> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("skill_id", skillId);
        updateWrapper.set("is_deleted", 1);
        characterSkillDao.update(null, updateWrapper);
        return new MsgOperation("1", "删除角色技能成功");
    }
}
